package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李旭
 * @date 2022/1/11 10:39
 * @Description: 首页
 * http://www.gmall.com/
 */
@Controller
@SuppressWarnings("all")
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;

    //人为触发的 生成指定的静态化页面
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml(){
        //数据
        List<Map> list1Map = getData();
        //封装数据的对象
        Context context = new Context();
        context.setVariable("list",list1Map);
        //输出流   编码
        Writer out = null;
        try {
            //out = new OutputStreamWriter(
              //      new FileOutputStream("D:\\temp\\index.html"), "utf-8");//写

            out = new PrintWriter("D:\\temp\\index.html","utf-8");
            //对首页进行静态化处理  Thymeleaf： 模板技术
            //参数1：模板
            //参数2：数据
            //参数3：输出 （哪里
            //进行静态化处理
            templateEngine.process("index/index",context,out);//读 UTF-8
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != out)
                   out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.ok();
    }

    /*@GetMapping("/")
    public String index(Model model) {
        //分类视图集合
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();

        List<Map> list1Map = baseCategoryViewList.stream().map(baseCategoryView1 -> {
            Map category1Result = new HashMap();
            //二级
            List<Map> list2Map = baseCategoryViewList.stream().map(baseCategoryView2 -> {
                Map category2Result = new HashMap();
                category2Result.put("categoryId", baseCategoryView2.getCategory2Id());
                category2Result.put("categoryName", baseCategoryView2.getCategory2Name());
                //三级分类集合
                List<Map> list3Map = baseCategoryViewList.stream().map(baseCategoryView3 -> {
                    Map category3Result = new HashMap();
                    category3Result.put("categoryId", baseCategoryView3.getCategory3Id());
                    category3Result.put("categoryName", baseCategoryView3.getCategory3Name());

                    return category3Result;

                }).collect(Collectors.toList());

                category2Result.put("categoryChild", list3Map);

                return category2Result;

            }).collect(Collectors.toList());
//            category1Result.put("index", baseCategoryView1.getCategory1Id());
            category1Result.put("categoryId", baseCategoryView1.getCategory1Id());
            category1Result.put("categoryName", baseCategoryView1.getCategory1Name());
            category1Result.put("categoryChild", list2Map);

            return category1Result;

        }).collect(Collectors.toList());

        System.out.println("list1Map = " + list1Map);
        model.addAttribute("list",list1Map);
        return "index/index";
    }*/
    //进入首页
    @GetMapping("/")
    public String index(Model model) {
        List<Map> list1Map = getData();
        model.addAttribute("list",list1Map);
        return "index/index";
    }

    //获取三级分类的数据
    private List<Map> getData() {
        //分类视图集合
        List<BaseCategoryView> baseCategoryViewList =
                productFeignClient.getBaseCategoryViewList();
        //以下代码 要求强大业务逻辑开发能力
        List<Map> list1Map = new ArrayList<>();

        //Stream流 分组  Long:category1Id  1 :  List<BaseCategoryView>:60
        //Stream流 分组  Long:category1Id  2 :  List<BaseCategoryView>:85 - 60
        //Stream流 分组  Long:category1Id  3 :  List<BaseCategoryView>:```
        Map<Long, List<BaseCategoryView>> category1IdMap = baseCategoryViewList.stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdEntry : category1IdMap.entrySet()) {
            //一级分类
            List<BaseCategoryView> category2IdList = category1IdEntry.getValue();
            Map category1Result = new HashMap();
            category1Result.put("index", ++index);
            category1Result.put("categoryId", category1IdEntry.getKey());
            category1Result.put("categoryName", category2IdList.get(0).getCategory1Name());
            //////////////////////////////////////////////////////////////////////
            //二级分类集合
            List<Map> list2Map = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> category2IdMap = category2IdList.stream().
                    collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdEntry : category2IdMap.entrySet()) {
                List<BaseCategoryView> category3IdList = category2IdEntry.getValue();
                Map category2Result = new HashMap();
                category2Result.put("categoryId", category2IdEntry.getKey());
                category2Result.put("categoryName", category3IdList.get(0).getCategory2Name());
                //////////////////////////////////////////////////////////////////
                //三级分类集合
                List<Map> list3Map = category3IdList.stream().map(baseCategoryView3 -> {
                    Map category3Result = new HashMap();
                    category3Result.put("categoryId", baseCategoryView3.getCategory3Id());
                    category3Result.put("categoryName", baseCategoryView3.getCategory3Name());
                    return category3Result;
                }).collect(Collectors.toList());
                category2Result.put("categoryChild",list3Map);
                list2Map.add(category2Result);
            }
            category1Result.put("categoryChild", list2Map);
            list1Map.add(category1Result);
        }
        return list1Map;
    }


}
